package com.mamahao.vmnetframework.framework.net

import com.mamahao.vmnetframework.framework.net.basis.callback.RequestMultiplyCallback
import com.mamahao.vmnetframework.framework.net.basis.exception.base.BaseException
import com.mamahao.vmnetframework.framework.net.service.MMHService
import com.mamahao.vmnetframework.framework.viewmodel.BaseViewModel
import kotlinx.coroutines.*

class RequestWrapper<T>:IHttpInterface<T> {

    var baseViewModel:BaseViewModel? = null

    override var _method: (MMHService.() -> Deferred<T>) = { throw IllegalAccessException("必须传入请求方法") }
    override var _showProgressBar:Boolean = false
    private var _start: (() -> Unit) = {}
    private var _success: (T) -> Unit = {}
    private var _fail: (BaseException) -> Unit = {}
    private var _finish: (() -> Unit) = {}
    var _listener: RequestListener? = null

    override fun onStart(onStart: () -> Unit) {
        _start = onStart
    }

    override fun onFail(onError: (BaseException) -> Unit) {
        _fail = onError
    }

    override fun onSuccess(onSuccess: (T) -> Unit) {
        _success = onSuccess
    }

    override fun onFinish(onFinish: () -> Unit) {
        _finish = onFinish
    }

    fun excute() {
        //前置回调
        _start()
        _listener?.onStart()
        if(_showProgressBar){
            baseViewModel?.startLoading()
        }
        //网络请求
        baseViewModel?.run {
            lifecycleOwner?.let {
                dataSource.apply {
                    execute(getService()._method(),object : RequestMultiplyCallback<T> {
                        override fun onFail(e: BaseException) {
                            //隐藏loadding
                            baseViewModel?.dismissLoading()
                            //失败回调
                            _fail(e)
                            _listener?.onFail(e)
                            //请求结束回调
                            _finish()
                            _listener?.onFinish()
                        }

                        override fun onSuccess(t: T) {
                            //隐藏loadding
                            baseViewModel?.dismissLoading()
                            //成功回调
                            _success(t)
                            if(t != null && t is Any){
                                _listener?.onSuccess(t)
                            }
                            //请求结束回调
                            _finish()
                            _listener?.onFinish()
                        }
                    })
                }
            }
        }
    }
}

interface RequestListener{

    fun onStart()

    fun onFail(baseException: BaseException)

    fun onSuccess(data: Any)

    fun onFinish()

}

object Http {

    fun <T> call(viewModel:BaseViewModel,actions:IHttpInterface<T>.() -> Unit){
        RequestWrapper<T>().apply {
            baseViewModel = viewModel
            actions()// 执行闭包，完成数据填充
            excute()// 添加到执行队列，自动执行
        }
    }

    fun call(viewModel:BaseViewModel, listener:RequestListener, actions:IHttpInterface<Any>.() -> Unit){
        RequestWrapper<Any>().apply {
            _listener = listener
            baseViewModel = viewModel
            actions()// 执行闭包，完成数据填充
            excute()// 添加到执行队列，自动执行
        }
    }

}
